// view/index/mingpian/mingpian.js
let app = getApp()
let yuming = app.globalData.yuming
import QRCode from '../../utils/weapp-qrcode.js';
const QR = require('../../utils/qrcode.js')
import {
  base64src
} from '../../utils/base64.js'
const request = require('../../config/request');
var context = null;
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    heights: "",
    title: '收款码',
    ewmImg: '',
    shopName: '',
    jgs: '',
    qrcodeURL: "",
    codeText: 'https://msion.86wky.com/H5alipay/index.html?shopId=' + wx.getStorageSync('shopId') + '&type=0',
    // shouma: yuming + 'static/applet/images/index/erwei.png'
  },
  // 商户信息
  shxx() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Besall/findShopInfos', {
      shopId: wx.getStorageSync('shopId'),
    }, "POST").then(res => {
      wx.hideLoading()
      var data = res.data.data
      this.setData({
        jgs: data.Office,
      })
    })
  },
  setText: function (e) {
    this.setData({
      codeText: e.detail.value
    })
  },
  // 绘制二维码
  drawImg: function () {
    var imgData = QR.drawImg(this.data.codeText, {
      typeNumber: 4,
      errorCorrectLevel: 'M',
      size: 500
    })
    this.setData({
      qrcodeURL: imgData
    })
    this.drawCanvasImg()
  },
  // 绘制图片
  drawCanvasImg() {
    var that = this
    var myCanvasWidth = 200 //为了让图片满铺页面
    var myCanvasHeight = 200
    context = wx.createCanvasContext('canvas');
    wx.getSystemInfo({
      success: function (res) {
        base64src(that.data.qrcodeURL, res => {
          console.log(res) // 返回图片地址
          context.drawImage(res, 0, 0, 200, 200); //画布绘制图片
          context.draw();
        });
      },
    })
  },
  // 保存二维码
  saveImg: function () { //保存图片
    wx.showLoading({
      title: '保存中...',
    });
    wx.canvasToTempFilePath({
      canvasId: 'canvas',
      fileType: 'png',
      success: function (res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            console.log(res)
            wx.hideLoading();
            wx.showToast({
              title: '保存成功',
            });
            // //存入服务器
            // wx.uploadFile({
            //  url: \'\', //接口地址
            //  filePath: res.tempFilePath,
            //  name: \'file\',
            //  formData: {                 //HTTP 请求中其他额外的 form data 
            //   \'user\': \'test\'
            //  },
            //  success: function (res) {
            //   console.log(res);

            //  },
            //  fail: function (res) {
            //   console.log(res);
            //  },
            //  complete: function (res) {
            //  }
            // });
          },
          fail() {
            wx.hideLoading()
          },
        })
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.drawImg();
    this.shxx();
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title,
      shopName: wx.getStorageSync('shopname')
    })
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})